package com.cloud.clouduser.security.common.resulthandler;

import com.cloud.cloudcommons.utils.CommonCheck;
import com.cloud.clouduser.security.base.LoginType;
import com.cloud.clouduser.security.base.SecurityPorperties;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Aurher: QiaoHang
 * @Description:
 * @Data: 2019/9/23 10:39
 * @Modified By:
 */
@Component
public class CommonAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    SecurityPorperties securityPorperties;

    /**
     * 用户授权认证成功==>触发
     * @param request
     * @param response
     * @param authentication
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request,
                                        HttpServletResponse response,
                                        Authentication authentication) throws IOException, ServletException {
        //是否走默认配置的相应类型(html/json)
        boolean flag = true;
        String requiredType = checkGetRequestHeader(request, "Occur-Authentication-Type");
        if(requiredType!=null) {
            if (!requiredType.equalsIgnoreCase(securityPorperties.getBrowser().getLoginType().toString())) {
                flag = false;
            }
        }
        if(flag){
            //走默认的响应类型
            if(LoginType.JSON.equals(securityPorperties.getBrowser().getLoginType())){
                //配置的登陆方式为 JOSN，直接响应json
                response.setStatus(HttpStatus.OK.value());
                response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
                response.getWriter().write(objectMapper.writeValueAsString(authentication));
            }else {
                //登陆方式为 HTML，继续走完formLogin()流程
                super.onAuthenticationSuccess(request,response,authentication);
            }
        }else {
            //走请求头指定的响应类型
            if("JSON".equalsIgnoreCase(requiredType)){
                response.setStatus(HttpStatus.OK.value());
                response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
                response.getWriter().write(objectMapper.writeValueAsString(authentication));
            }else {
                super.onAuthenticationSuccess(request,response,authentication);
            }
        }
    }


    //获取指定请求头
    public String checkGetRequestHeader(HttpServletRequest request,String header){
        String requestHeader = request.getHeader(header);
        if(CommonCheck.nullStrings(requestHeader))
            return null;
        return requestHeader;
    }
}
